home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Mac Game Programming Gurus / TricksOfTheMacGameProgrammingGurus.iso / More Source / C⁄C++ / Peter's Final Project / jpeg-5b / jdmaster.c < prev    next >
Text File  |  1995-03-10  |  24KB  |  654 lines

  1. /*
  2.  * jdmaster.c
  3.  *
  4.  * Copyright (C) 1991-1995, Thomas G. Lane.
  5.  * This file is part of the Independent JPEG Group's software.
  6.  * For conditions of distribution and use, see the accompanying README file.
  7.  *
  8.  * This file contains master control logic for the JPEG decompressor.
  9.  * These routines are concerned with selecting the modules to be executed
  10.  * and with determining the number of passes and the work to be done in each
  11.  * pass.
  12.  */
  13.  
  14. #define JPEG_INTERNALS
  15. #include "jinclude.h"
  16. #include "jpeglib.h"
  17.  
  18.  
  19. /* Private state */
  20.  
  21. typedef enum {
  22.     main_pass,        /* read and process a single-scan file */
  23.     preread_pass,        /* read one scan of a multi-scan file */
  24.     output_pass,        /* primary processing pass for multi-scan */
  25.     post_pass        /* optional post-pass for 2-pass quant. */
  26. } D_PASS_TYPE;
  27.  
  28. typedef struct {
  29.   struct jpeg_decomp_master pub; /* public fields */
  30.  
  31.   boolean using_merged_upsample; /* TRUE if using merged upsample/cconvert */
  32.  
  33.   D_PASS_TYPE pass_type;    /* the type of the current pass */
  34.  
  35.   int pass_number;        /* # of passes completed */
  36.   int total_passes;        /* estimated total # of passes needed */
  37.  
  38.   boolean need_post_pass;    /* are we using full two-pass quantization? */
  39. } my_decomp_master;
  40.  
  41. typedef my_decomp_master * my_master_ptr;
  42.  
  43.  
  44. /*
  45.  * Determine whether merged upsample/color conversion should be used.
  46.  * CRUCIAL: this must match the actual capabilities of jdmerge.c!
  47.  */
  48.  
  49. LOCAL boolean
  50. use_merged_upsample (j_decompress_ptr cinfo)
  51. {
  52. #ifdef UPSAMPLE_MERGING_SUPPORTED
  53.   /* Merging is the equivalent of plain box-filter upsampling */
  54.   if (cinfo->do_fancy_upsampling || cinfo->CCIR601_sampling)
  55.     return FALSE;
  56.   /* jdmerge.c only supports YCC=>RGB color conversion */
  57.   if (cinfo->jpeg_color_space != JCS_YCbCr || cinfo->num_components != 3 ||
  58.       cinfo->out_color_space != JCS_RGB ||
  59.       cinfo->out_color_components != RGB_PIXELSIZE)
  60.     return FALSE;
  61.   /* and it only handles 2h1v or 2h2v sampling ratios */
  62.   if (cinfo->comp_info[0].h_samp_factor != 2 ||
  63.       cinfo->comp_info[1].h_samp_factor != 1 ||
  64.       cinfo->comp_info[2].h_samp_factor != 1 ||
  65.       cinfo->comp_info[0].v_samp_factor >  2 ||
  66.       cinfo->comp_info[1].v_samp_factor != 1 ||
  67.       cinfo->comp_info[2].v_samp_factor != 1)
  68.     return FALSE;
  69.   /* furthermore, it doesn't work if we've scaled the IDCTs differently */
  70.   if (cinfo->comp_info[0].DCT_scaled_size != cinfo->min_DCT_scaled_size ||
  71.       cinfo->comp_info[1].DCT_scaled_size != cinfo->min_DCT_scaled_size ||
  72.       cinfo->comp_info[2].DCT_scaled_size != cinfo->min_DCT_scaled_size)
  73.     return FALSE;
  74.   /* ??? also need to test for upsample-time rescaling, when & if supported */
  75.   /* by golly, it'll work... */
  76.   return TRUE;
  77. #else
  78.   return FALSE;
  79. #endif
  80. }
  81.  
  82.  
  83. /*
  84.  * Support routines that do various essential calculations.
  85.  *
  86.  * jpeg_calc_output_dimensions is exported for possible use by application.
  87.  * Hence it mustn't do anything that can't be done twice.
  88.  */
  89.  
  90. GLOBAL void
  91. jpeg_calc_output_dimensions (j_decompress_ptr cinfo)
  92. /* Do computations that are needed before master selection phase */
  93. {
  94.   int ci;
  95.   jpeg_component_info *compptr;
  96.  
  97.   /* Compute maximum sampling factors; check factor validity */
  98.   cinfo->max_h_samp_factor = 1;
  99.   cinfo->max_v_samp_factor = 1;
  100.   for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
  101.        ci++, compptr++) {
  102.     if (compptr->h_samp_factor<=0 || compptr->h_samp_factor>MAX_SAMP_FACTOR ||
  103.     compptr->v_samp_factor<=0 || compptr->v_samp_factor>MAX_SAMP_FACTOR)
  104.       ERREXIT(cinfo, JERR_BAD_SAMPLING);
  105.     cinfo->max_h_samp_factor = MAX(cinfo->max_h_samp_factor,
  106.                    compptr->h_samp_factor);
  107.     cinfo->max_v_samp_factor = MAX(cinfo->max_v_samp_factor,
  108.                    compptr->v_samp_factor);
  109.   }
  110.  
  111.   /* Compute actual output image dimensions and DCT scaling choices. */
  112. #ifdef IDCT_SCALING_SUPPORTED
  113.   if (cinfo->scale_num * 8 <= cinfo->scale_denom) {
  114.     /* Provide 1/8 scaling */
  115.     cinfo->output_width = (JDIMENSION)
  116.       jdiv_round_up((long) cinfo->image_width, 8L);
  117.     cinfo->output_height = (JDIMENSION)
  118.       jdiv_round_up((long) cinfo->image_height, 8L);
  119.     cinfo->min_DCT_scaled_size = 1;
  120.   } else if (cinfo->scale_num * 4 <= cinfo->scale_denom) {
  121.     /* Provide 1/4 scaling */
  122.     cinfo->output_width = (JDIMENSION)
  123.       jdiv_round_up((long) cinfo->image_width, 4L);
  124.     cinfo->output_height = (JDIMENSION)
  125.       jdiv_round_up((long) cinfo->image_height, 4L);
  126.     cinfo->min_DCT_scaled_size = 2;
  127.   } else if (cinfo->scale_num * 2 <= cinfo->scale_denom) {
  128.     /* Provide 1/2 scaling */
  129.     cinfo->output_width = (JDIMENSION)
  130.       jdiv_round_up((long) cinfo->image_width, 2L);
  131.     cinfo->output_height = (JDIMENSION)
  132.       jdiv_round_up((long) cinfo->image_height, 2L);
  133.     cinfo->min_DCT_scaled_size = 4;
  134.   } else {
  135.     /* Provide 1/1 scaling */
  136.     cinfo->output_width = cinfo->image_width;
  137.     cinfo->output_height = cinfo->image_height;
  138.     cinfo->min_DCT_scaled_size = DCTSIZE;
  139.   }
  140.   /* In selecting the actual DCT scaling for each component, we try to
  141.    * scale up the chroma components via IDCT scaling rather than upsampling.
  142.    * This saves time if the upsampler gets to use 1:1 scaling.
  143.    * Note this code assumes that the supported DCT scalings are powers of 2.
  144.    */
  145.   for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
  146.        ci++, compptr++) {
  147.     int ssize = cinfo->min_DCT_scaled_size;
  148.     while (ssize < DCTSIZE &&
  149.        (compptr->h_samp_factor * ssize * 2 <=
  150.         cinfo->max_h_samp_factor * cinfo->min_DCT_scaled_size) &&
  151.        (compptr->v_samp_factor * ssize * 2 <=
  152.         cinfo->max_v_samp_factor * cinfo->min_DCT_scaled_size)) {
  153.       ssize = ssize * 2;
  154.     }
  155.     compptr->DCT_scaled_size = ssize;
  156.   }
  157. #else /* !IDCT_SCALING_SUPPORTED */
  158.   /* Hardwire it to "no scaling" */
  159.   cinfo->output_width = cinfo->image_width;
  160.   cinfo->output_height = cinfo->image_height;
  161.   cinfo->min_DCT_scaled_size = DCTSIZE;
  162.   for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
  163.        ci++, compptr++) {
  164.     compptr->DCT_scaled_size = DCTSIZE;
  165.   }
  166. #endif /* IDCT_SCALING_SUPPORTED */
  167.  
  168.   /* Report number of components in selected colorspace. */
  169.   /* Probably this should be in the color conversion module... */
  170.   switch (cinfo->out_color_space) {
  171.   case JCS_GRAYSCALE:
  172.     cinfo->out_color_components = 1;
  173.     break;
  174.   case JCS_RGB:
  175. #if RGB_PIXELSIZE != 3
  176.     cinfo->out_color_components = RGB_PIXELSIZE;
  177.     break;
  178. #endif /* else share code with YCbCr */
  179.   case JCS_YCbCr:
  180.     cinfo->out_color_components = 3;
  181.     break;
  182.   case JCS_CMYK:
  183.   case JCS_YCCK:
  184.     cinfo->out_color_components = 4;
  185.     break;
  186.   default:            /* else must be same colorspace as in file */
  187.     cinfo->out_color_components = cinfo->num_components;
  188.     break;
  189.   }
  190.   cinfo->output_components = (cinfo->quantize_colors ? 1 :
  191.                   cinfo->out_color_components);
  192.  
  193.   /* See if upsampler will want to emit more than one row at a time */
  194.   if (use_merged_upsample(cinfo))
  195.     cinfo->rec_outbuf_height = cinfo->max_v_samp_factor;
  196.   else
  197.     cinfo->rec_outbuf_height = 1;
  198.  
  199.   /* Compute various sampling-related dimensions.
  200.    * Some of these are of interest to the application if it is dealing with
  201.    * "raw" (not upsampled) output, so we do the calculations here.
  202.    */
  203.  
  204.   /* Compute dimensions of components */
  205.   for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
  206.        ci++, compptr++) {
  207.     /* Size in DCT blocks */
  208.     compptr->width_in_blocks = (JDIMENSION)
  209.       jdiv_round_up((long) cinfo->image_width * (long) compptr->h_samp_factor,
  210.             (long) (cinfo->max_h_samp_factor * DCTSIZE));
  211.     compptr->height_in_blocks = (JDIMENSION)
  212.       jdiv_round_up((long) cinfo->image_height * (long) compptr->v_samp_factor,
  213.             (long) (cinfo->max_v_samp_factor * DCTSIZE));
  214.     /* Size in samples, after IDCT scaling */
  215.     compptr->downsampled_width = (JDIMENSION)
  216.       jdiv_round_up((long) cinfo->image_width *
  217.             (long) (compptr->h_samp_factor * compptr->DCT_scaled_size),
  218.             (long) (cinfo->max_h_samp_factor * DCTSIZE));
  219.     compptr->downsampled_height = (JDIMENSION)
  220.       jdiv_round_up((long) cinfo->image_height *
  221.             (long) (compptr->v_samp_factor * compptr->DCT_scaled_size),
  222.             (long) (cinfo->max_v_samp_factor * DCTSIZE));
  223.     /* Mark component needed, until color conversion says otherwise */
  224.     compptr->component_needed = TRUE;
  225.   }
  226.  
  227.   /* Compute number of fully interleaved MCU rows (number of times that
  228.    * main controller will call coefficient controller).
  229.    */
  230.   cinfo->total_iMCU_rows = (JDIMENSION)
  231.     jdiv_round_up((long) cinfo->image_height,
  232.           (long) (cinfo->max_v_samp_factor*DCTSIZE));
  233. }
  234.  
  235.  
  236. LOCAL void
  237. per_scan_setup (j_decompress_ptr cinfo)
  238. /* Do computations that are needed before processing a JPEG scan */
  239. /* cinfo->comps_in_scan and cinfo->cur_comp_info[] were set from SOS marker */
  240. {
  241.   int ci, mcublks, tmp;
  242.   jpeg_component_info *compptr;
  243.   
  244.   if (cinfo->comps_in_scan == 1) {
  245.     
  246.     /* Noninterleaved (single-component) scan */
  247.     compptr = cinfo->cur_comp_info[0];
  248.     
  249.     /* Overall image size in MCUs */
  250.     cinfo->MCUs_per_row = compptr->width_in_blocks;
  251.     cinfo->MCU_rows_in_scan = compptr->height_in_blocks;
  252.     
  253.     /* For noninterleaved scan, always one block per MCU */
  254.     compptr->MCU_width = 1;
  255.     compptr->MCU_height = 1;
  256.     compptr->MCU_blocks = 1;
  257.     compptr->MCU_sample_width = compptr->DCT_scaled_size;
  258.     compptr->last_col_width = 1;
  259.     /* For noninterleaved scans, it is convenient to define last_row_height
  260.      * as the number of block rows present in the last iMCU row.
  261.      */
  262.     tmp = (int) (compptr->height_in_blocks % compptr->v_samp_factor);
  263.     if (tmp == 0) tmp = compptr->v_samp_factor;
  264.     compptr->last_row_height = tmp;
  265.     
  266.     /* Prepare array describing MCU composition */
  267.     cinfo->blocks_in_MCU = 1;
  268.     cinfo->MCU_membership[0] = 0;
  269.     
  270.   } else {
  271.     
  272.     /* Interleaved (multi-component) scan */
  273.     if (cinfo->comps_in_scan <= 0 || cinfo->comps_in_scan > MAX_COMPS_IN_SCAN)
  274.       ERREXIT2(cinfo, JERR_COMPONENT_COUNT, cinfo->comps_in_scan,
  275.            MAX_COMPS_IN_SCAN);
  276.     
  277.     /* Overall image size in MCUs */
  278.     cinfo->MCUs_per_row = (JDIMENSION)
  279.       jdiv_round_up((long) cinfo->image_width,
  280.             (long) (cinfo->max_h_samp_factor*DCTSIZE));
  281.     cinfo->MCU_rows_in_scan = (JDIMENSION)
  282.       jdiv_round_up((long) cinfo->image_height,
  283.             (long) (cinfo->max_v_samp_factor*DCTSIZE));
  284.     
  285.     cinfo->blocks_in_MCU = 0;
  286.     
  287.     for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
  288.       compptr = cinfo->cur_comp_info[ci];
  289.       /* Sampling factors give # of blocks of component in each MCU */
  290.       compptr->MCU_width = compptr->h_samp_factor;
  291.       compptr->MCU_height = compptr->v_samp_factor;
  292.       compptr->MCU_blocks = compptr->MCU_width * compptr->MCU_height;
  293.       compptr->MCU_sample_width = compptr->MCU_width * compptr->DCT_scaled_size;
  294.       /* Figure number of non-dummy blocks in last MCU column & row */
  295.       tmp = (int) (compptr->width_in_blocks % compptr->MCU_width);
  296.       if (tmp == 0) tmp = compptr->MCU_width;
  297.       compptr->last_col_width = tmp;
  298.       tmp = (int) (compptr->height_in_blocks % compptr->MCU_height);
  299.       if (tmp == 0) tmp = compptr->MCU_height;
  300.       compptr->last_row_height = tmp;
  301.       /* Prepare array describing MCU composition */
  302.       mcublks = compptr->MCU_blocks;
  303.       if (cinfo->blocks_in_MCU + mcublks > MAX_BLOCKS_IN_MCU)
  304.     ERREXIT(cinfo, JERR_BAD_MCU_SIZE);
  305.       while (mcublks-- > 0) {
  306.     cinfo->MCU_membership[cinfo->blocks_in_MCU++] = ci;
  307.       }
  308.     }
  309.     
  310.   }
  311. }
  312.  
  313.  
  314. /*
  315.  * Several decompression processes need to range-limit values to the range
  316.  * 0..MAXJSAMPLE; the input value may fall somewhat outside this range
  317.  * due to noise introduced by quantization, roundoff error, etc.  These
  318.  * processes are inner loops and need to be as fast as possible.  On most
  319.  * machines, particularly CPUs with pipelines or instruction prefetch,
  320.  * a (subscript-check-less) C table lookup
  321.  *        x = sample_range_limit[x];
  322.  * is faster than explicit tests
  323.  *        if (x < 0)  x = 0;
  324.  *        else if (x > MAXJSAMPLE)  x = MAXJSAMPLE;
  325.  * These processes all use a common table prepared by the routine below.
  326.  *
  327.  * For most steps we can mathematically guarantee that the initial value
  328.  * of x is within MAXJSAMPLE+1 of the legal range, so a table running from
  329.  * -(MAXJSAMPLE+1) to 2*MAXJSAMPLE+1 is sufficient.  But for the initial
  330.  * limiting step (just after the IDCT), a wildly out-of-range value is 
  331.  * possible if the input data is corrupt.  To avoid any chance of indexing
  332.  * off the end of memory and getting a bad-pointer trap, we perform the
  333.  * post-IDCT limiting thus:
  334.  *        x = range_limit[x & MASK];
  335.  * where MASK is 2 bits wider than legal sample data, ie 10 bits for 8-bit
  336.  * samples.  Under normal circumstances this is more than enough range and
  337.  * a correct output will be generated; with bogus input data the mask will
  338.  * cause wraparound, and we will safely generate a bogus-but-in-range output.
  339.  * For the post-IDCT step, we want to convert the data from signed to unsigned
  340.  * representation by adding CENTERJSAMPLE at the same time that we limit it.
  341.  * So the post-IDCT limiting table ends up looking like this:
  342.  *   CENTERJSAMPLE,CENTERJSAMPLE+1,...,MAXJSAMPLE,
  343.  *   MAXJSAMPLE (repeat 2*(MAXJSAMPLE+1)-CENTERJSAMPLE times),
  344.  *   0          (repeat 2*(MAXJSAMPLE+1)-CENTERJSAMPLE times),
  345.  *   0,1,...,CENTERJSAMPLE-1
  346.  * Negative inputs select values from the upper half of the table after
  347.  * masking.
  348.  *
  349.  * We can save some space by overlapping the start of the post-IDCT table
  350.  * with the simpler range limiting table.  The post-IDCT table begins at
  351.  * sample_range_limit + CENTERJSAMPLE.
  352.  *
  353.  * Note that the table is allocated in near data space on PCs; it's small
  354.  * enough and used often enough to justify this.
  355.  */
  356.  
  357. LOCAL void
  358. prepare_range_limit_table (j_decompress_ptr cinfo)
  359. /* Allocate and fill in the sample_range_limit table */
  360. {
  361.   JSAMPLE * table;
  362.   int i;
  363.  
  364.   table = (JSAMPLE *)
  365.     (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
  366.         (5 * (MAXJSAMPLE+1) + CENTERJSAMPLE) * SIZEOF(JSAMPLE));
  367.   table += (MAXJSAMPLE+1);    /* allow negative subscripts of simple table */
  368.   cinfo->sample_range_limit = table;
  369.   /* First segment of "simple" table: limit[x] = 0 for x < 0 */
  370.   MEMZERO(table - (MAXJSAMPLE+1), (MAXJSAMPLE+1) * SIZEOF(JSAMPLE));
  371.   /* Main part of "simple" table: limit[x] = x */
  372.   for (i = 0; i <= MAXJSAMPLE; i++)
  373.     table[i] = (JSAMPLE) i;
  374.   table += CENTERJSAMPLE;    /* Point to where post-IDCT table starts */
  375.   /* End of simple table, rest of first half of post-IDCT table */
  376.   for (i = CENTERJSAMPLE; i < 2*(MAXJSAMPLE+1); i++)
  377.     table[i] = MAXJSAMPLE;
  378.   /* Second half of post-IDCT table */
  379.   MEMZERO(table + (2 * (MAXJSAMPLE+1)),
  380.       (2 * (MAXJSAMPLE+1) - CENTERJSAMPLE) * SIZEOF(JSAMPLE));
  381.   MEMCOPY(table + (4 * (MAXJSAMPLE+1) - CENTERJSAMPLE),
  382.       cinfo->sample_range_limit, CENTERJSAMPLE * SIZEOF(JSAMPLE));
  383. }
  384.  
  385.  
  386. /*
  387.  * Master selection of decompression modules.
  388.  * This is done once at the start of processing an image.  We determine
  389.  * which modules will be used and give them appropriate initialization calls.
  390.  *
  391.  * Note that this is called only after jpeg_read_header has finished.
  392.  * We therefore know what is in the SOF and (first) SOS markers.
  393.  */
  394.  
  395. LOCAL void
  396. master_selection (j_decompress_ptr cinfo)
  397. {
  398.   my_master_ptr master = (my_master_ptr) cinfo->master;
  399.   long samplesperrow;
  400.   JDIMENSION jd_samplesperrow;
  401.  
  402.   /* Initialize dimensions and other stuff */
  403.   jpeg_calc_output_dimensions(cinfo);
  404.   prepare_range_limit_table(cinfo);
  405.  
  406.   /* Width of an output scanline must be representable as JDIMENSION. */
  407.   samplesperrow = (long) cinfo->output_width * (long) cinfo->out_color_components;
  408.   jd_samplesperrow = (JDIMENSION) samplesperrow;
  409.   if ((long) jd_samplesperrow != samplesperrow)
  410.     ERREXIT(cinfo, JERR_WIDTH_OVERFLOW);
  411.  
  412.   /* Initialize my private state */
  413.   master->pub.eoi_processed = FALSE;
  414.   master->pass_number = 0;
  415.   master->need_post_pass = FALSE;
  416.   if (cinfo->comps_in_scan == cinfo->num_components) {
  417.     master->pass_type = main_pass;
  418.     master->total_passes = 1;
  419.   } else {
  420. #ifdef D_MULTISCAN_FILES_SUPPORTED
  421.     master->pass_type = preread_pass;
  422.     /* Assume there is a separate scan for each component; */
  423.     /* if partially interleaved, we'll increment pass_number appropriately */
  424.     master->total_passes = cinfo->num_components + 1;
  425. #else
  426.     ERREXIT(cinfo, JERR_NOT_COMPILED);
  427. #endif
  428.   }
  429.   master->using_merged_upsample = use_merged_upsample(cinfo);
  430.  
  431.   /* There's not a lot of smarts here right now, but it'll get more
  432.    * complicated when we have multiple implementations available...
  433.    */
  434.  
  435.   /* Color quantizer selection */
  436.   if (cinfo->quantize_colors) {
  437.     if (cinfo->raw_data_out)
  438.       ERREXIT(cinfo, JERR_NOTIMPL);
  439. #ifdef QUANT_2PASS_SUPPORTED
  440.     /* 2-pass quantizer only works in 3-component color space.
  441.      * We use the "2-pass" code in a single pass if a colormap is given.
  442.      */
  443.     if (cinfo->out_color_components != 3)
  444.       cinfo->two_pass_quantize = FALSE;
  445.     else if (cinfo->colormap != NULL)
  446.       cinfo->two_pass_quantize = TRUE;
  447. #else
  448.     /* Force 1-pass quantize if we don't have 2-pass code compiled. */
  449.     cinfo->two_pass_quantize = FALSE;
  450. #endif
  451.  
  452.     if (cinfo->two_pass_quantize) {
  453. #ifdef QUANT_2PASS_SUPPORTED
  454.       if (cinfo->colormap == NULL) {
  455.     master->need_post_pass = TRUE;
  456.     master->total_passes++;
  457.       }
  458.       jinit_2pass_quantizer(cinfo);
  459. #else
  460.       ERREXIT(cinfo, JERR_NOT_COMPILED);
  461. #endif
  462.     } else {
  463. #ifdef QUANT_1PASS_SUPPORTED
  464.       jinit_1pass_quantizer(cinfo);
  465. #else
  466.       ERREXIT(cinfo, JERR_NOT_COMPILED);
  467. #endif
  468.     }
  469.   }
  470.  
  471.   /* Post-processing: in particular, color conversion first */
  472.   if (! cinfo->raw_data_out) {
  473.     if (master->using_merged_upsample) {
  474. #ifdef UPSAMPLE_MERGING_SUPPORTED
  475.       jinit_merged_upsampler(cinfo); /* does color conversion too */
  476. #else
  477.       ERREXIT(cinfo, JERR_NOT_COMPILED);
  478. #endif
  479.     } else {
  480.       jinit_color_deconverter(cinfo);
  481.       jinit_upsampler(cinfo);
  482.     }
  483.     jinit_d_post_controller(cinfo, master->need_post_pass);
  484.   }
  485.   /* Inverse DCT */
  486.   jinit_inverse_dct(cinfo);
  487.   /* Entropy decoding: either Huffman or arithmetic coding. */
  488.   if (cinfo->arith_code) {
  489. #ifdef D_ARITH_CODING_SUPPORTED
  490.     jinit_arith_decoder(cinfo);
  491. #else
  492.     ERREXIT(cinfo, JERR_ARITH_NOTIMPL);
  493. #endif
  494.   } else
  495.     jinit_huff_decoder(cinfo);
  496.  
  497.   jinit_d_coef_controller(cinfo, (master->pass_type == preread_pass));
  498.   jinit_d_main_controller(cinfo, FALSE /* never need full buffer here */);
  499.   /* Note that main controller is initialized even in raw-data mode. */
  500.  
  501.   /* We can now tell the memory manager to allocate virtual arrays. */
  502.   (*cinfo->mem->realize_virt_arrays) ((j_common_ptr) cinfo);
  503. }
  504.  
  505.  
  506. /*
  507.  * Per-pass setup.
  508.  * This is called at the beginning of each pass.  We determine which modules
  509.  * will be active during this pass and give them appropriate start_pass calls.
  510.  * We also set is_last_pass to indicate whether any more passes will be
  511.  * required.
  512.  */
  513.  
  514. METHODDEF void
  515. prepare_for_pass (j_decompress_ptr cinfo)
  516. {
  517.   my_master_ptr master = (my_master_ptr) cinfo->master;
  518.  
  519.   switch (master->pass_type) {
  520.   case main_pass:
  521.     /* Set up to read and decompress single-scan file in one pass */
  522.     per_scan_setup(cinfo);
  523.     master->pub.is_last_pass = ! master->need_post_pass;
  524.     if (! cinfo->raw_data_out) {
  525.       if (! master->using_merged_upsample)
  526.     (*cinfo->cconvert->start_pass) (cinfo);
  527.       (*cinfo->upsample->start_pass) (cinfo);
  528.       if (cinfo->quantize_colors)
  529.     (*cinfo->cquantize->start_pass) (cinfo, master->need_post_pass);
  530.       (*cinfo->post->start_pass) (cinfo,
  531.         (master->need_post_pass ? JBUF_SAVE_AND_PASS : JBUF_PASS_THRU));
  532.     }
  533.     (*cinfo->idct->start_input_pass) (cinfo);
  534.     (*cinfo->idct->start_output_pass) (cinfo);
  535.     (*cinfo->entropy->start_pass) (cinfo);
  536.     (*cinfo->coef->start_pass) (cinfo, JBUF_PASS_THRU);
  537.     (*cinfo->main->start_pass) (cinfo, JBUF_PASS_THRU);
  538.     break;
  539. #ifdef D_MULTISCAN_FILES_SUPPORTED
  540.   case preread_pass:
  541.     /* Read (another) scan of a multi-scan file */
  542.     per_scan_setup(cinfo);
  543.     master->pub.is_last_pass = FALSE;
  544.     (*cinfo->idct->start_input_pass) (cinfo);
  545.     (*cinfo->entropy->start_pass) (cinfo);
  546.     (*cinfo->coef->start_pass) (cinfo, JBUF_SAVE_SOURCE);
  547.     (*cinfo->main->start_pass) (cinfo, JBUF_CRANK_SOURCE);
  548.     break;
  549.   case output_pass:
  550.     /* All scans read, now do the IDCT and subsequent processing */
  551.     master->pub.is_last_pass = ! master->need_post_pass;
  552.     if (! cinfo->raw_data_out) {
  553.       if (! master->using_merged_upsample)
  554.     (*cinfo->cconvert->start_pass) (cinfo);
  555.       (*cinfo->upsample->start_pass) (cinfo);
  556.       if (cinfo->quantize_colors)
  557.     (*cinfo->cquantize->start_pass) (cinfo, master->need_post_pass);
  558.       (*cinfo->post->start_pass) (cinfo,
  559.         (master->need_post_pass ? JBUF_SAVE_AND_PASS : JBUF_PASS_THRU));
  560.     }
  561.     (*cinfo->idct->start_output_pass) (cinfo);
  562.     (*cinfo->coef->start_pass) (cinfo, JBUF_CRANK_DEST);
  563.     (*cinfo->main->start_pass) (cinfo, JBUF_PASS_THRU);
  564.     break;
  565. #endif /* D_MULTISCAN_FILES_SUPPORTED */
  566. #ifdef QUANT_2PASS_SUPPORTED
  567.   case post_pass:
  568.     /* Final pass of 2-pass quantization */
  569.     master->pub.is_last_pass = TRUE;
  570.     (*cinfo->cquantize->start_pass) (cinfo, FALSE);
  571.     (*cinfo->post->start_pass) (cinfo, JBUF_CRANK_DEST);
  572.     (*cinfo->main->start_pass) (cinfo, JBUF_CRANK_DEST);
  573.     break;
  574. #endif /* QUANT_2PASS_SUPPORTED */
  575.   default:
  576.     ERREXIT(cinfo, JERR_NOT_COMPILED);
  577.   }
  578.  
  579.   /* Set up progress monitor's pass info if present */
  580.   if (cinfo->progress != NULL) {
  581.     cinfo->progress->completed_passes = master->pass_number;
  582.     cinfo->progress->total_passes = master->total_passes;
  583.   }
  584. }
  585.  
  586.  
  587. /*
  588.  * Finish up at end of pass.
  589.  * In multi-scan mode, we must read next scan header and set the next
  590.  * pass_type correctly for prepare_for_pass.
  591.  */
  592.  
  593. METHODDEF void
  594. finish_pass_master (j_decompress_ptr cinfo)
  595. {
  596.   my_master_ptr master = (my_master_ptr) cinfo->master;
  597.  
  598.   switch (master->pass_type) {
  599.   case main_pass:
  600.   case output_pass:
  601.     if (cinfo->quantize_colors)
  602.       (*cinfo->cquantize->finish_pass) (cinfo);
  603.     master->pass_number++;
  604.     master->pass_type = post_pass; /* in case need_post_pass is true */
  605.     break;
  606. #ifdef D_MULTISCAN_FILES_SUPPORTED
  607.   case preread_pass:
  608.     /* Count one pass done for each component in this scan */
  609.     master->pass_number += cinfo->comps_in_scan;
  610.     switch ((*cinfo->marker->read_markers) (cinfo)) {
  611.     case JPEG_HEADER_OK:    /* Found SOS, do another preread pass */
  612.       break;
  613.     case JPEG_HEADER_TABLES_ONLY: /* Found EOI, no more preread passes */
  614.       master->pub.eoi_processed = TRUE;
  615.       master->pass_type = output_pass;
  616.       break;
  617.     case JPEG_SUSPENDED:
  618.       ERREXIT(cinfo, JERR_CANT_SUSPEND);
  619.     }
  620.     break;
  621. #endif /* D_MULTISCAN_FILES_SUPPORTED */
  622. #ifdef QUANT_2PASS_SUPPORTED
  623.   case post_pass:
  624.     (*cinfo->cquantize->finish_pass) (cinfo);
  625.     /* there will be no more passes, don't bother to change state */
  626.     break;
  627. #endif /* QUANT_2PASS_SUPPORTED */
  628.   default:
  629.     ERREXIT(cinfo, JERR_NOT_COMPILED);
  630.   }
  631. }
  632.  
  633.  
  634. /*
  635.  * Initialize master decompression control.
  636.  * This creates my own subrecord and also performs the master selection phase,
  637.  * which causes other modules to create their subrecords.
  638.  */
  639.  
  640. GLOBAL void
  641. jinit_master_decompress (j_decompress_ptr cinfo)
  642. {
  643.   my_master_ptr master;
  644.  
  645.   master = (my_master_ptr)
  646.       (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
  647.                   SIZEOF(my_decomp_master));
  648.   cinfo->master = (struct jpeg_decomp_master *) master;
  649.   master->pub.prepare_for_pass = prepare_for_pass;
  650.   master->pub.finish_pass = finish_pass_master;
  651.  
  652.   master_selection(cinfo);
  653. }
  654.